package modelo.hibernate.dao;

import modelo.entidad.*;

import java.util.ArrayList;
import java.util.List;

import modelo.hibernate.config.Sesion;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

public class DaoVehiculo {
	
	private Sesion sesionPostgres;
	
	public void agregarProfesor(Vehiculo dato) throws Exception{
		@SuppressWarnings("static-access")
		Session em = sesionPostgres.openSession();  
         Transaction tx = null;  
         try {    
        	 tx = em.beginTransaction();
              em.save( dato);   
              tx.commit();  
         } catch (Exception e) {  
             tx.rollback();            
             e.printStackTrace();
             throw e;
         } finally {  
             em.close();  
         } 
	}
	
	public void actualizarVehiculo(Vehiculo dato) throws Exception{
		@SuppressWarnings("static-access")
		Session em = sesionPostgres.openSession();  
         Transaction tx = null;  
         try {    
        	 tx = em.beginTransaction();
              em.update(dato);   
              tx.commit();  
         } catch (Exception e) {  
             tx.rollback();            
             e.printStackTrace();
             throw e;
         } finally {  
             em.close();  
         } 
	}
	
	public Vehiculo obtenerVehiculo(int id) throws Exception{		 
	    @SuppressWarnings("static-access")
		Session sesion = sesionPostgres.openSession();  
            Vehiculo dato = null;        
            try{
            	dato = (Vehiculo) sesion.get(Vehiculo.class,  id);
            } catch (Exception e) {  
            e.printStackTrace();
           
            throw new Exception(e.getMessage(),e.getCause());
            }  finally {  
                sesion.close();  
            }  
           
	    return dato;
}
	
	public Vehiculo findVehiculoporPlaca(String placa) throws Exception{
		Vehiculo dato = new Vehiculo();
		Session em = sesionPostgres.openSession();
		try
		{
			dato = (Vehiculo)em.createQuery("SELECT p FROM Vehiculo p WHERE placa= '"+placa+"'").uniqueResult();
			
		}catch (Exception e) {             
		       
	         throw new Exception(e.getMessage(),e.getCause());
	        } finally {  
	          em.close();  
	        }
		return dato;
	}
	
	public List<Vehiculo> obtenerTodos() throws Exception {            
	      
		List<Vehiculo> datos = new ArrayList<Vehiculo>();  
	  Session em = sesionPostgres.openSession();  	
        try {  	
	    datos =  (List<Vehiculo>) em.createCriteria(Vehiculo.class).list();             
        } catch (Exception e) {             
       
         throw new Exception(e.getMessage(),e.getCause());
        } finally {  
          em.close();  
        } 
       
        return datos; 
	}
	
	public void actualizarCit(int id_ciudad, int id_vehiculo) throws Exception{
		@SuppressWarnings("static-access")
		Session em = sesionPostgres.openSession();  
         Transaction tx = null;  
         try {    
        	 em.createQuery("UPDATE Vehiculo SET id_ciudad ='" +id_ciudad+"' WHERE id = '" + id_vehiculo + "'");
         } catch (Exception e) {  
             tx.rollback();            
             e.printStackTrace();
             throw e;
         } finally {  
             em.close();  
         } 
	}

}
